package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.ProducePriceManagementEccution;
import com.fortune.dto.in.producePriceManagement.EditPriceDealerDto;
import com.fortune.dto.in.producePriceManagement.PriceDealerListDto;
import com.fortune.dto.in.producePriceManagement.ProduceDealerListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.ProducePriceManagementEnum;
import com.fortune.service.DAccountInfoService;
import com.fortune.service.DProductWholesalePriceService;
import com.fortune.vo.producePriceManagement.PriceManagementDealer;
import com.fortune.vo.producePriceManagement.ProductWholesalePriceList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "product", description = "商品价格管理", tags = {"商品价格管理相关"})
@RestController
@RequestMapping("productPrice")
public class ProducePriceManagementController {
    @Autowired
    private DAccountInfoService dAccountInfoService;
    @Autowired
    private DProductWholesalePriceService dProductWholesalePriceService;

    @ApiOperation(value = "经销商列表", nickname = "dealerList", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="search", value="搜索条件",paramType="query",dataType="string")
    })
    @GetMapping(value = "dealerList",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<List<PriceManagementDealer>> list(HttpServletRequest request, @ApiIgnore @Validated PriceDealerListDto priceDealerListDto) {
//        String companyId = "1";
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        List<PriceManagementDealer> list = dAccountInfoService.queryAccounPricetList(companyId,priceDealerListDto);
        return new ProducePriceManagementEccution<List<PriceManagementDealer>>(list, ProducePriceManagementEnum.SUCCESS);
    }

    @ApiOperation(value = "商品列表", nickname = "mProductList", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
            @ApiImplicitParam(name="accountId", value="经销商id",paramType="query",dataType="string")
    })
    @PostMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<ProductWholesalePriceList>> produceList(HttpServletRequest request, @ApiIgnore @Validated ProduceDealerListDto produceDealerListDto) {
//        String companyId = "1";
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        produceDealerListDto.setPage((produceDealerListDto.getPage()-1)*10);
        List<ProductWholesalePriceList> lists = dProductWholesalePriceService.queryDealerProduceInfo(companyId,produceDealerListDto);
        int count = dProductWholesalePriceService.queryDealerProduceInfoCount(companyId,produceDealerListDto);
        PagingData<ProductWholesalePriceList> pagingData = new PagingData<>();
        pagingData.setData(lists);
        pagingData.setRecordsTotal(count);
        return new ProducePriceManagementEccution<PagingData<ProductWholesalePriceList>>(pagingData, ProducePriceManagementEnum.SUCCESS);
    }


    @ApiOperation(value = "修改优惠批发价", nickname = "mProductEdit", notes = "" ,tags = {"@石匡"})
    @PutMapping(value = "edit",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<List<ProductWholesalePriceList>> editPrice(HttpServletRequest request,@Validated @RequestBody EditPriceDealerDto editPriceDealerDto) {
//        String companyId = "1";
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num = dProductWholesalePriceService.editPrice(companyId,editPriceDealerDto);
        if (num < 1){
            return new ProducePriceManagementEccution<List<ProductWholesalePriceList>>(null, ProducePriceManagementEnum.FAILURE);
        }
        return new ProducePriceManagementEccution<List<ProductWholesalePriceList>>(null, ProducePriceManagementEnum.SUCCESS);
    }





}
